<?php
if(!check_manager_login()) exit('无权查看，请登录!');
require_once(LIB_PATH.DS.'upload.class.php');
require_once(MODEL_PATH.DS.'Brand.php');
$Brand = new Brand();
$bid = isset($_GET['brand_id'])?$_GET['brand_id']:'';
if(check_post()){

	$name = trim($_POST['name']);
	$sort = trim($_POST['sort_order']);
	$messages = array();
	$error = false;
	if(empty($name)){
		$error = true;
		$messages[] = "品牌名称不能为空!";
	}
	$data = array();
	
	if(!empty($_FILES['image'])){
		$arr_tmp = $Brand->getBrandImageByIds($bid);
		if(isset($arr_tmp)){
			foreach($arr_tmp as $arr){
				$file_name = ADMIN_PATH.DS.'uploads'.DS.$arr['image'];
				if(file_exists($file_name)){
					@unlink($file_name);
				}
			}
		}
	}
	if((!$error)){
		$data['id'] = $bid;
		$data['name'] = $name;
		$data['sort_order'] = $sort;
		$Upload = new Upload('uploads/');
		$Upload->move();
		$messages = $Upload->getMessages();
		$image = $Upload->getFilename();
		if($image){
			$data['image'] = $image[0];
		}
		$result = $Brand->updateBrand($data);
		if($result){
			redirect('index.php?mod=brand&act=list','更新成功！');
		}
	}else{
		$messages[] = "更新失败!";
	}
	
}


$brand = $Brand->getBrandById($bid);

include load_tpl('brand_add');
?>